/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.uefs.cedoc.db.tipologia;

/**
 *
 * @author suporte
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import br.uefs.cedoc.db.DBController;
import br.uefs.cedoc.db.exception.DBConnectionException;
import br.uefs.cedoc.entidades.TipologiaBean;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Gregory Porto
 */
public class DBTipologia {

    public void criarTipologia(TipologiaBean tipologia) throws SQLException, DBConnectionException {

        String comandoSQL = "INSERT INTO tipologia (tipologia) VALUES(?)";

        Connection conn = DBController.getInstance().getConnection();

        if (conn != null) {

            PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);
            prepStatement.setString(1, tipologia.getTipologia());
            //DBOperacoes dbo = new DBOperacoes();
            //tipologia.setIdtipologia(dbo.getMaxID("idtipologia", "tipologia"));	
            //System.out.println("IDSECAO= "+tipologia.getIdtipologia());
            //prepStatement.setInt(1, tipologia.getIdtipologia());
            prepStatement.execute();
		
            prepStatement.close();
        } else {
            DBConnectionException dbException = new DBConnectionException(" Erro de conexao com banco ");
            dbException.printStackTrace();
            throw dbException;
        }




    }

    public synchronized void editarTipologia(TipologiaBean tipologia) throws SQLException, DBConnectionException {

        String comandoSQL = "UPDATE tipologia SET tipologia = ? WHERE idtipologia = ?";

        Connection conn = DBController.getInstance().getConnection();
        //System.out.println("Editar acervo: "+acervo.getNome());
        if (conn != null) {

            PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);
            prepStatement.setString(1, tipologia.getTipologia());
            prepStatement.setInt(2, tipologia.getIdTipologia());
            prepStatement.execute();
            prepStatement.close();

        } else {
            DBConnectionException dbException = new DBConnectionException(" Erro de conex�o com banco ");
            dbException.printStackTrace();
            throw dbException;
        }


    }

    public TipologiaBean listaTipologia(int id) throws SQLException, DBConnectionException {
        String comandoSQL = "SELECT * FROM tipologia WHERE idtipologia=?"; // incluir
        // limites

        Connection conn = DBController.getInstance().getConnection();
        try {
            if (conn != null) {
                PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);

                prepStatement.setInt(1,id);
                ResultSet rs = prepStatement.executeQuery();



                while (rs.next()) {
                    TipologiaBean tipologia = new TipologiaBean();

                    tipologia.setIdTipologia(rs.getInt("idtipologia"));
                    tipologia.setTipologia(rs.getString("tipologia"));
                    
                    return tipologia;

                }
                DBController.getInstance().closeAll(conn, null, null);
               


            } else {
                DBConnectionException dbException = new DBConnectionException(" Erro de conex�o com banco ");
                dbException.printStackTrace();
                throw dbException;
            }
            return null;
        } catch (NullPointerException ex) {
            ex.printStackTrace();
            return null;
        }
    }



    	public  void removerTipologia(int id) throws SQLException {

		String comandoSQL = "DELETE FROM tipologia WHERE idtipologia=?";

		Connection conn = DBController.getInstance().getConnection();
		
		if (conn != null){
			

			PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);
			prepStatement.setInt(1, id);						
			prepStatement.execute();			
			prepStatement.close();
		}	

	}

    public TipologiaBean[] listarTipologias() throws SQLException, DBConnectionException {
        String comandoSQL = "SELECT * FROM tipologia "; // incluir
        // limites

        Connection conn = DBController.getInstance().getConnection();
        try {
            if (conn != null) {
                PreparedStatement prepStatement = conn.prepareStatement(comandoSQL);


                ResultSet rs = prepStatement.executeQuery();

                Collection secoes = new ArrayList();

                while (rs.next()) {
                    TipologiaBean tipologia_temp = new TipologiaBean();

                    tipologia_temp.setIdTipologia(rs.getInt("idtipologia"));
                    tipologia_temp.setTipologia(rs.getString("tipologia"));
                    secoes.add(tipologia_temp);

                }
                DBController.getInstance().closeAll(conn, null, null);
                return (TipologiaBean[]) secoes.toArray(new TipologiaBean[0]);


            } else {
                DBConnectionException dbException = new DBConnectionException(" Erro de conex�o com banco ");
                dbException.printStackTrace();
                throw dbException;
            }
        } catch (NullPointerException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}
